<template>
    <div>
        <van-sticky>
            <van-nav-bar title="登录" left-text="返回" left-arrow @click-left="onClickLeft" right-text="注册"
                @click-right="onClickRight" />
        </van-sticky>
        <div class="title">
            欢迎登录智慧城市
        </div>
        <van-form @submit="onSubmit">
            <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]" />
            <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]" />
            <div style="margin: 16px;">
                <van-button round block type="info" native-type="submit">登录</van-button>
            </div>
        </van-form>
    </div>
</template>
<script>
import { userLogin, userAddress } from "@/service/user/userLogin";
import nav from "@/mixin/nav";
export default {
    mixins: [nav],
    data() {
        return {
            username: '',
            password: '',
        };
    },
    methods: {
        async onSubmit(values) {
            console.log('submit', values);
            let { data } = await userLogin(values);
            console.log(data);
            if (data.code == 200) {
                this.$toast.success("登录成功");
                let { data: list } = await userAddress();
                console.log(list);
                localStorage.setItem("address", JSON.stringify(list.data));
                localStorage.setItem("token", data.token);
                var pre = localStorage.getItem("preRoute");
                console.log(pre);
                this.$router.push({ path: pre });
            } else {
                this.$toast.fail(data.msg);
            }
        },
        onClickLeft() {
            this.$router.push("/");
        },
        onClickRight() {
            this.$router.push("/register");
        }
    },
};

</script>
<style scoped lang="scss">
.title {
    font-size: 30px;
    line-height: 50px;
    margin: 30px;
}
</style>